package com.example.demo_mysql_mybatisplus.mybatisplus.controller;


import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.baomidou.dynamic.datasource.creator.*;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.Set;

/**
 * Describe class function detail.
 *
 * @author Tara
 * @version v1.0
 * @date 2021 -03-18 17:14:37
 * @since jdk1.8
 */
@RestController
@AllArgsConstructor
@RequestMapping("/datasources")
public class DynamicDataSourceController {

    /**
     * Data source
     */
    @Autowired
    private final DataSource dataSource;
    /**
     * Data source creator
     */
    @Resource
    private final DefaultDataSourceCreator dataSourceCreator;
    /**
     * Basic data source creator
     */
    @Resource
    private final BasicDataSourceCreator basicDataSourceCreator;
    /**
     * Jndi data source creator
     */
    @Resource
    private final JndiDataSourceCreator jndiDataSourceCreator;
    /**
     * Druid data source creator
     */
    @Resource
    private final DruidDataSourceCreator druidDataSourceCreator;
    /**
     * Hikari data source creator
     */
    @Resource
    private final HikariDataSourceCreator hikariDataSourceCreator;

    /**
     * 获取当前所有数据源
     *
     * @return set set
     */
    @GetMapping
    public Set<String> now() {
        DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
        return ds.getDataSources().keySet();
    }

    /**
     * 通用添加数据源（推荐）
     * 测试数据 配合查询方法 com.aliyun.gts.bpass.dynamic.datasource.controller.UserController#getDatasourceData(java.lang.String)
     *
     * {
     *     "pollName": "Druid",
     *     "driverClassName": "com.mysql.cj.jdbc.Driver",
     *     "url": "jdbc:mysql://localhost:3306/dynamic_datasource_test?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=false",
     *     "username": "root",
     *     "password": "root"
     * }
     *
     * @param dto dto
     * @return set set
     */
    @PostMapping("/add")
    public Set<String> add(@Validated @RequestBody DataSourceProperty dto) {
        DataSourceProperty dataSourceProperty = new DataSourceProperty();
        BeanUtils.copyProperties(dto, dataSourceProperty);
        DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
        DataSource dataSource = dataSourceCreator.createDataSource(dataSourceProperty);
        ds.addDataSource(dto.getPoolName(), dataSource);
        return ds.getDataSources().keySet();
    }


    /**
     * 添加基础数据源
     * 调用Springboot内置方法创建数据源，兼容1,2
     *
     * @param dto dto
     * @return the set
     */
    @PostMapping("/addBasic")
    public Set<String> addBasic(@Validated @RequestBody DataSourceProperty dto) {
        DataSourceProperty dataSourceProperty = new DataSourceProperty();
        BeanUtils.copyProperties(dto, dataSourceProperty);
        DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
        DataSource dataSource = basicDataSourceCreator.createDataSource(dataSourceProperty);
        ds.addDataSource(dto.getPoolName(), dataSource);
        return ds.getDataSources().keySet();
    }

    /**
     * 添加JNDI数据源
     *
     * @param pollName poll name
     * @param jndiName jndi name
     * @return the set
     */
    @PostMapping("/addJndi")
    public Set<String> addJndi(String pollName, String jndiName) {
        DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
        DataSource dataSource = jndiDataSourceCreator.createDataSource(jndiName);
        ds.addDataSource(pollName, dataSource);
        return ds.getDataSources().keySet();
    }

    /**
     * 基础Druid数据源
     *
     * @param dto dto
     * @return the set
     */
    @PostMapping("/addDruid")
    public Set<String> addDruid(@Validated @RequestBody DataSourceProperty dto) {
        DataSourceProperty dataSourceProperty = new DataSourceProperty();
        BeanUtils.copyProperties(dto, dataSourceProperty);
        DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
        DataSource dataSource = druidDataSourceCreator.createDataSource(dataSourceProperty);
        ds.addDataSource(dto.getPoolName(), dataSource);
        return ds.getDataSources().keySet();
    }

    /**
     * 基础HikariCP数据源
     *
     * @param dto dto
     * @return the set
     */
    @PostMapping("/addHikariCP")
    public Set<String> addHikariCP(@Validated @RequestBody DataSourceProperty dto) {
        DataSourceProperty dataSourceProperty = new DataSourceProperty();
        BeanUtils.copyProperties(dto, dataSourceProperty);
        DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
        DataSource dataSource = hikariDataSourceCreator.createDataSource(dataSourceProperty);
        ds.addDataSource(dto.getPoolName(), dataSource);
        return ds.getDataSources().keySet();
    }

    /**
     * 删除数据源
     *
     * @param name name
     * @returnurn the string
     */
    @DeleteMapping
    public String remove(String name) {
        DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
        ds.removeDataSource(name);
        return "删除成功";
    }
}